import tensorflow as tf
from zh.model.mnist.mlp import MLP
from zh.model.utils import MNISTLoader

# @see https://tf.wiki/zh_hans/basic/tools.html#id1
# 命令行输入
# “ tensorboard --logdir=./tensorboard  ”
# 以启动 tensorboard


num_batches = 1000
batch_size = 50
learning_rate = 0.001
log_dir = 'tensorboard'
model = MLP()
data_loader = MNISTLoader()
optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
summary_writer = tf.summary.create_file_writer(log_dir)  # 实例化记录器
# 开启Trace，可以记录图结构和profile信息（均为可选）
# 注意：如果这里要记录graph信息，则需要使用 @tf.function 以图执行模式执行代码才会有计算图显示出来，默认的即时执行模式是没有计算图的。
tf.summary.trace_on(profiler=True)
for batch_index in range(num_batches):
    X, y = data_loader.get_batch(batch_size)
    with tf.GradientTape() as tape:
        y_pred = model(X)
        loss = tf.keras.losses.sparse_categorical_crossentropy(y_true=y, y_pred=y_pred)
        loss = tf.reduce_mean(loss)
        print("batch %d: loss %f" % (batch_index, loss.numpy()))
        with summary_writer.as_default():  # 指定记录器
            tf.summary.scalar("loss", loss, step=batch_index)  # 将当前损失函数的值写入记录器
    grads = tape.gradient(loss, model.variables)
    optimizer.apply_gradients(grads_and_vars=zip(grads, model.variables))
with summary_writer.as_default():
    tf.summary.trace_export(name="model_trace", step=0, profiler_outdir=log_dir)  # 保存Trace信息到文件（可选）
